//
//  HeaderController.m
//  MaxMovie
//
//  Created by Maxdong on 14-12-29.
//  Copyright (c) 2014年 lanou3g.com 蓝欧科技. All rights reserved.
//

#import "HeaderController.h"
#import "NetworkHandler.h"
#import "TopNRCell.h"
#import "TopModel.h"
#import "HeaderDetailViewController.h"
@interface HeaderController ()<UIWebViewDelegate,UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, retain)UITableView *myTableView;

@end

@implementation HeaderController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self =[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.myArr =[NSMutableArray array];
        self.getData =[NSMutableArray array];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor =[UIColor whiteColor];
    
    
    
    
    //滚动视图
    UIScrollView *BigScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height - 44)];
    
    BigScrollView.showsVerticalScrollIndicator = FALSE;
    BigScrollView.showsHorizontalScrollIndicator = FALSE;
    [self.view addSubview:BigScrollView];
    [BigScrollView release];
    
    //左button
    UIButton * leftb =[UIButton buttonWithType:UIButtonTypeSystem];
    leftb.frame =CGRectMake(20, 20, 20, 20);
    UIImage * img1 =[UIImage imageNamed:@"left.png"];
    [leftb setBackgroundImage:img1 forState:UIControlStateNormal];
    [leftb addTarget:self action:@selector(leftbAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:leftb];

    
    //网络请求
    [NetworkHandler connectioonNetGETDataWithURLString:self.heaStr completion:^(id result) {
        
        [_getData removeAllObjects];
        [_getData addObjectsFromArray:[result objectForKey:@"movies"]];
        [self.myTableView reloadData];
    
    }];
    self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height - 100) style:UITableViewStylePlain];
    _myTableView.dataSource = self;
    _myTableView.delegate = self;
    [_myTableView registerClass:[TopNRCell class] forCellReuseIdentifier:@"topNR"];
    
    [BigScrollView addSubview:_myTableView];
    self.myTableView = _myTableView;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 240;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 
    return [self.getData count];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"headerVc";
    TopNRCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[TopNRCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str] ;
    }
    
    
    NSDictionary *dic = [self.getData objectAtIndex:indexPath.row];
    NSURL *url =[NSURL URLWithString:[dic objectForKey:@"posterUrl"]];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage * img =[UIImage imageWithData:data];
    cell.postView.image=img;
    
    
    
    [cell.nameLabel setText:[dic objectForKey:@"name"]];
    
    
    NSString * ns1 =[NSString stringWithFormat:@"(%@)",[dic objectForKey:@"decade"]];
    [cell.decadeLabel setText:ns1];
    
    
    double dou =[[dic objectForKey:@"rating"] doubleValue];
    NSString * ns2 =[NSString stringWithFormat:@"%.1f分",dou];
    [cell.ratingLabel setText:ns2];
    
    
    NSString * ns3 =[NSString stringWithFormat:@"导演: %@",[dic objectForKey:@"director"]];
    [cell.directorLabel setText:ns3];
    
    
    NSString * ns4 =[NSString stringWithFormat:@"演员: %@",[dic objectForKey:@"actor"]];
    [cell.actorLabel setText:ns4];
    
    
    NSString * ns5 =[NSString stringWithFormat:@"类型: %@",[dic objectForKey:@"movieType"]];
    [cell.movieTypeLabel setText:ns5];
    
    
    NSString * ns6 =[NSString stringWithFormat:@"%@",[dic objectForKey:@"releaseDate"]];
    [cell.releaseDateLabel setText:ns6];
    
    NSString * ns8 =[NSString stringWithFormat:@"%@",[dic objectForKey:@"releaseLocation"]];
    [cell.releaseLocationLabel setText:ns8];
    
    NSString * ns7 =[NSString stringWithFormat:@"简介: %@",[dic objectForKey:@"remark"]];
    [cell.remarkLabel setText:ns7];
    
    
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    
    NSString * hedstr =[[self.getData objectAtIndex:indexPath.row]valueForKey:@"id"];
    NSString * name =[[self.getData objectAtIndex:indexPath.row]valueForKey:@"name"];
    
    HeaderDetailViewController * headdel=[[HeaderDetailViewController alloc]init];
    headdel.title =name;
    
    NSString * idStr =[NSString stringWithFormat:@"http://api.m.mtime.cn/Showtime/MovieDetail.api?movieId=%@",hedstr];
    headdel.myUrl =idStr;
    [self.navigationController pushViewController:headdel animated:YES];
    
    
    
   
    
    
}

//左右点击事件
- (void)leftbAction:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
